const { Model, DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    class ExchangeGood extends Model {
        static associate(models) {
            // 定义关联关系
            ExchangeGood.hasMany(models.ExchangeOrder, {
                foreignKey: 'goods_id',
                as: 'orders'
            });
        }
    }

    ExchangeGood.init(
        {
            id: {
                type: DataTypes.BIGINT,
                autoIncrement: true,
                primaryKey: true,
                comment: '商品ID'
            },
            name: {
                type: DataTypes.STRING(100),
                allowNull: false,
                comment: '商品名称'
            },
            description: {
                type: DataTypes.TEXT,
                comment: '商品描述'
            },
            image_url: {
                type: DataTypes.STRING(255),
                comment: '商品图片URL'
            },
            points: {
                type: DataTypes.INTEGER,
                allowNull: false,
                comment: '所需积分'
            },
            stock: {
                type: DataTypes.INTEGER,
                defaultValue: 0,
                comment: '库存数量'
            },
            status: {
                type: DataTypes.TINYINT,
                defaultValue: 1,
                comment: '状态：1-上架，0-下架'
            },
            deleted_at: DataTypes.DATE
        },
        {
            sequelize,
            modelName: 'ExchangeGood',
            tableName: 'exchange_goods',
            timestamps: true,
            paranoid: true,
            underscored: true
        }
    );

    return ExchangeGood;
};